/**
 * <p>带有选择机构功能的机构显示grid</p>
 * xtype = cceafselectgroupgridpanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.SelectGroupGridPanel
 * @extends Ext.grid.GridPanel
 * @since 1.0
 */
Ext.define('cceaf.control.SelectGroupGridPanel',{
	extend: 'Ext.grid.GridPanel', 
    alias: 'widget.cceafselectgroupgridpanel',
    
	constructor: function(config){
		config = config || {};
		
		var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
			pluginId: 'pluginIdOfcceafdetailviewgridpanel',
            clicksToEdit: 1
        });
		
		//定义操作区
		var dockedItems;
		var edit = config.edit;
		if ( !Ext.isDefined(edit) ) {
			edit = true;
		}
		
		if (edit) {
			dockedItems = [{
				dock: 'top', 
				xtype: 'toolbar',
				items: [
					{itemId: 'btnDeleteRow', iconCls: 'deleteIcon', text: ' 删除组织机构 ', pressed: true, handler: function(){
						//获取要删除对象的主键集
						var billGrid = this.up('gridpanel');
				    	var selModel = billGrid.getSelectionModel();
				    	var selectedCount = selModel.getCount();
				    	if(selectedCount == 0){
				    		cceaf.util.ExtHelper.showInfoDialog('请选择要删除的行');
				    		return;
				    	}
				    	else{
				    		//确定要删除对象
				    		Ext.MessageBox.show({
								title:'确认提示框',
								modal: true,
								msg: '确信要删除您选中的行吗？',
								minWidth: 200,
								buttons: Ext.MessageBox.YESNO,
								icon: Ext.MessageBox.QUESTION,
								fn:function(btn){
									if(btn == 'yes'){
										var selections = selModel.getSelection();
										cellEditing.cancelEdit();  
										billGrid.getStore().remove(selections);  
									}
								}
							});
				    	}
					  }
					},
					{xtype: 'tbfill'},
					{itemId: 'btnAddRow', iconCls: 'addIcon', text: ' 添加组织机构 ', pressed: true, handler: function(){
							if (config.id == null) {
								cceaf.util.ExtHelper.showInfoDialog('必须设置选择机构表格的id！');
								return;
							}
							var multiSelectGroupWin = Ext.getCmp('_multiSelectGroupWin');
							if (!multiSelectGroupWin) {
								multiSelectGroupWin = new Ext.Window({
				    	    		id:'_multiSelectGroupWin',
				    	        	title: '选择组织机构',
				    	        	modal: true,
				    	        	closeAction: 'hide',
				    	        	height: 400,
				    	        	width: 300,
				    	        	autoScroll: true,
				    	        	layout: 'fit',
				    	        	items: [
				    	        		new cceaf.control.MultiSelectGroup({
				    	        			fillGridId: config.id,
				    	        			model: config.model,
				    	        			submitData: function(checkedRecords){
				    	        				var multiSelectGroup = this;
						               			var fillGridTree = Ext.getCmp(multiSelectGroup.fillGridId);
												var count = fillGridTree.getStore().getCount();
												
						               			Ext.Array.each(checkedRecords, function(record){
						               				//原store中是否存在同样的数据
						               				var isSame = multiSelectGroup.isExistSame(fillGridTree.getStore(), "groupId", record.get('id'));
													
													if (!isSame) {
														var recordData = Ext.create(multiSelectGroup.model, fillGridTree.getFillRecord(record) );
												        fillGridTree.getStore().insert(count,recordData);
											        }
							                    });
							                    
											}
											
				    	        		})
				    	        	]
				    			});
			    			}
	    					
			    			multiSelectGroupWin.show();
						}
					}
				]
			}];
		
		}
		
		var store;
		if (config.model == null) {
			cceaf.util.ExtHelper.showInfoDialog('必须设置选择机构表格的model！');
		} else {
			store = new Ext.data.Store({model: config.model});
		}
		
		config = Ext.applyIf(config || {}, {
			store: store,
			selModel: new Ext.selection.CheckboxModel(),
			colspan: 5,
	        border: true,
	        frame: true,
	        //disableSelection: false, // 允许选择行 *  
	        selType: 'cellmodel', // 选择类型设置为：单元格选择  
	        plugins: [cellEditing],
	        autoScroll: true,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        dockedItems: dockedItems,
	        listeners : {  
	            'selectionchange' : function(sm, selections) {  
	            	if (this.down('#btnDeleteRow')) {
	                	this.down('#btnDeleteRow').setDisabled(selections.length == 0);  
	                }
	            }  
	        }
		});

		cceaf.control.SelectGroupGridPanel.superclass.constructor.apply(this, arguments);
	},
	
	/**
	* 从选择机构树的record中获取需要填充到该grid的数据，类似：<br>
	* <br> {
	* <br> 	 groupId: record.get('id'),
	* <br> 	 groupName: record.get('text')
	* <br> }
	*/
	getFillRecord : function(record){
		cceaf.util.ExtHelper.showInfoDialog('请在cceafselectgroupgridpanel中重载方法:getFillRecord');
		
		return {};
		/*
		return {
			groupId: record.get('id'),
			groupName: record.get('text')
		};
		*/
    },
	
	
	initComponent : function(){
		cceaf.control.SelectGroupGridPanel.superclass.initComponent.call(this);
    },

    initEvents: function() {
    	cceaf.control.SelectGroupGridPanel.superclass.initEvents.call(this);
    }
    
});